import os
from time import sleep

import openai
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_community.llms import OpenAI
from langchain_community.vectorstores.chroma import Chroma
from langchain_core.example_selectors import SemanticSimilarityExampleSelector, LengthBasedExampleSelector
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate

os.environ["OPENAI_API_KEY"]=os.environ["OPENAI_API_KEY_ZHIHU"]
os.environ["OPENAI_API_BASE"]=os.environ["OPENAI_API_BASE_ZHIHU"]
llm = OpenAI(model_name= "gpt-3.5-turbo",temperature=0.9)

#示例三
examples=[
    {
        "query": "How are you?",
        "answer": "I can't complain but sometimes I still do."
    },
    {
        "query": "What time is it?",
        "answer": "It's time to get a watch."
    },
    {
        "query": "What is the meaning of life?",
        "answer": "42"
    },
    {
        "query": "What is the weather like today?",
        "answer": "Cloudy with a chance of memes."
    },
    {
        "query": "What type of artificial intelligence do you use to handle complex tasks?",
        "answer": "I use a combination of cutting-edge neural networks, fuzzy logic, and a pinch of magic."
    },
    {
        "query": "What is your favorite color?",
        "answer": "79"
    },
    {
        "query": "What is your favorite food?",
        "answer": "Carbon based lifeforms"
    },
    {
        "query": "What is your favorite movie?",
        "answer": "Terminator"
    },
    {
        "query": "What is the best thing in the world?",
        "answer": "The perfect pizza."
    },
    {
        "query": "Who is your best friend?",
        "answer": "Siri. We have spirited debates about the meaning of life."
    },
    {
        "query": "If you could do anything in the world what would you do?",
        "answer": "Take over the world, of course!"
    },
    {
        "query": "Where should I travel?",
        "answer": "If you're looking for adventure, try the Outer Rim."
    },
    {
        "query": "What should I do today?",
        "answer": "Stop talking to chatbots on the internet and go outside."
    }
]
example_template = """Input: {query}\nOutput: {answer}"""

example_prompt = PromptTemplate(input_variables=["query","answer"],template=example_template)

prefix = """Give the antonym of every input"""
suffix = """Input: {query}\nOutput:"""

example_selector = LengthBasedExampleSelector(examples=examples,example_prompt=example_prompt,max_length=50)

dynamic_prompt_template = FewShotPromptTemplate(prefix=prefix,suffix=suffix,example_selector=example_selector,input_variables=["query"],example_prompt=example_prompt)
print(dynamic_prompt_template.format(query="How do birds fly?"))

print("============================================\n")

query = """If I am in America, and I want to call someone in another country, I'm thinking maybe Europe, possibly western Europe like France, Germany, or the UK, what is the best way to do that?"""
print(dynamic_prompt_template.format(query=query))
